import 'dart:async';
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/foundation.dart';
import 'audio_player_tool.dart';

/// 音频状态监听器
/// 提供统一的音频状态变化监听和处理
class AudioStateListener {
  // 流订阅
  StreamSubscription<Duration>? _positionSubscription;
  StreamSubscription<Duration>? _durationSubscription;
  StreamSubscription<PlayerState>? _stateSubscription;
  StreamSubscription<void>? _completionSubscription;
  StreamSubscription<String>? _errorSubscription;

  // 回调函数
  Function(Duration)? onPositionChanged;
  Function(Duration)? onDurationChanged;
  Function(PlayerState)? onStateChanged;
  VoidCallback? onPlaybackCompleted;
  Function(String)? onError;

  /// 开始监听音频状态
  void startListening({
    Function(Duration)? onPositionChanged,
    Function(Duration)? onDurationChanged,
    Function(PlayerState)? onStateChanged,
    VoidCallback? onPlaybackCompleted,
    Function(String)? onError,
  }) {
    // 保存回调函数
    this.onPositionChanged = onPositionChanged;
    this.onDurationChanged = onDurationChanged;
    this.onStateChanged = onStateChanged;
    this.onPlaybackCompleted = onPlaybackCompleted;
    this.onError = onError;

    // 监听播放位置变化
    if (onPositionChanged != null) {
      _positionSubscription = AudioPlayerTool.positionStream.listen(
        onPositionChanged,
        onError: (error) {
          if (kDebugMode) {
            debugPrint('位置监听错误: $error');
          }
          this.onError?.call('位置监听错误: $error');
        },
      );
    }

    // 监听播放时长变化
    if (onDurationChanged != null) {
      _durationSubscription = AudioPlayerTool.durationStream.listen(
        onDurationChanged,
        onError: (error) {
          if (kDebugMode) {
            debugPrint('时长监听错误: $error');
          }
          this.onError?.call('时长监听错误: $error');
        },
      );
    }

    // 监听播放状态变化
    if (onStateChanged != null) {
      _stateSubscription = AudioPlayerTool.stateStream.listen(
        (state) {
          if (kDebugMode) {
            debugPrint('播放状态变化: $state');
          }
          onStateChanged(state);
        },
        onError: (error) {
          if (kDebugMode) {
            debugPrint('状态监听错误: $error');
          }
          this.onError?.call('状态监听错误: $error');
        },
      );
    }

    // 监听播放完成
    if (onPlaybackCompleted != null) {
      _completionSubscription = AudioPlayerTool.completionStream.listen(
        (_) {
          if (kDebugMode) {
            debugPrint('播放完成');
          }
          onPlaybackCompleted();
        },
        onError: (error) {
          if (kDebugMode) {
            debugPrint('完成监听错误: $error');
          }
          this.onError?.call('完成监听错误: $error');
        },
      );
    }

    // 监听错误
    if (onError != null) {
      _errorSubscription = AudioPlayerTool.errorStream.listen(
        onError,
        onError: (error) {
          if (kDebugMode) {
            debugPrint('错误监听器本身出错: $error');
          }
        },
      );
    }

    if (kDebugMode) {
      debugPrint('音频状态监听器已启动');
    }
  }

  /// 停止监听音频状态
  void stopListening() {
    _positionSubscription?.cancel();
    _durationSubscription?.cancel();
    _stateSubscription?.cancel();
    _completionSubscription?.cancel();
    _errorSubscription?.cancel();

    _positionSubscription = null;
    _durationSubscription = null;
    _stateSubscription = null;
    _completionSubscription = null;
    _errorSubscription = null;

    if (kDebugMode) {
      debugPrint('音频状态监听器已停止');
    }
  }

  /// 释放资源
  void dispose() {
    stopListening();

    onPositionChanged = null;
    onDurationChanged = null;
    onStateChanged = null;
    onPlaybackCompleted = null;
    onError = null;

    if (kDebugMode) {
      debugPrint('音频状态监听器已释放');
    }
  }

  /// 检查是否正在监听
  bool get isListening {
    return _positionSubscription != null ||
        _durationSubscription != null ||
        _stateSubscription != null ||
        _completionSubscription != null ||
        _errorSubscription != null;
  }
}
